﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using YYTmEditer.MtObject;
using yyycommon;

namespace YYTmEditer
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class BlessingControll : UserControl
    {
        private List<Blessing> myBlessing;
        private List<Blessing> allBlessing;

        public BlessingControll()
        {
            InitializeComponent();
        }

        private void button_add_Click(object sender, RoutedEventArgs e)
        {
            Blessing select  = this.alllist.SelectedItem as Blessing;
            if (select != null)
            {
                this.myBlessing.Add(select);
                GameObject.Instance.SetBlessing(this.myBlessing);
                this.mylist.ItemsSource = null;
                this.mylist.ItemsSource = this.myBlessing;
            }
        }

        private void button_remove_Click(object sender, RoutedEventArgs e)
        {
            Blessing select = this.mylist.SelectedItem as Blessing;
            if (select != null)
            {
                this.myBlessing.Remove(select);
                GameObject.Instance.SetBlessing(this.myBlessing);
                this.mylist.ItemsSource = null;
                this.mylist.ItemsSource = this.myBlessing;
            }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            reload();
        }

        private void reload() { 
            allBlessing = new List<Blessing>();
            NameMap nameMap = NameMap.BlessName;
            foreach (var kp in nameMap.names) {
                Blessing blessing = new Blessing();
                blessing.relicDataID = kp.Key;
                blessing.name = kp.Value;
                allBlessing.Add(blessing);
            }
            List<Blessing> list = GameObject.Instance.GetBlessing();
            foreach(var b in list)
            {
                if (!nameMap.Contains(b.relicDataID))
                {
                    nameMap.SetValue(b.relicDataID, Properties.Resources.unkonw_item_name);
                }
                else {
                    b.name = nameMap.GetValue(b.relicDataID);
                }
            }
            myBlessing = list;
            Binding binding = new Binding();
            binding.Source = myBlessing;
            this.mylist.SetBinding(ListBox.ItemsSourceProperty, binding);
            this.alllist.ItemsSource = allBlessing;
        }

        private void mylist_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            var select = listBox.SelectedItem as Blessing;
            if (select != null) {
                var be = new BlessingEditer();
                var eo = new EditObject(select.name,select.relicDataID,"bless");
                be.SetBlessing(eo);
                be.ShowDialog();
                if (!string.IsNullOrEmpty(eo.name))
                {

                    listBox.IsSynchronizedWithCurrentItem = true;
                    NameMap.BlessName.SetValue(select.relicDataID, eo.ToString());
                    reload();
                }
            }
        }
    }
}
